#!/usr/bin/env python
#-*- encoding:utf-8 -*-
#
#       SysTray Icon
#
#       Kévin Gomez <geek63@gmail.com>
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

from PyQt4 import QtGui, QtCore

import gui.Dials as Dials
import libs.Config as Config


class SysTray(QtGui.QDialog):
    """ Class gérant le SysTray Icon """

    def __init__(self):
        super(QtGui.QDialog, self).__init__()

        #pour gérer l'utf8
        charset = QtCore.QTextCodec.codecForName('UTF-8')
        QtCore.QTextCodec.setCodecForCStrings(charset)
        QtCore.QTextCodec.setCodecForLocale(charset)
        QtCore.QTextCodec.setCodecForTr(charset)

        self.createTrayIcon()
        self.createActions()
        self.createContextualMenu()

    def createActions(self):
        self.exit = QtGui.QAction(QtGui.QIcon(Config.ICONS_DIR+'/exit.png'),
                    self.tr('Quitter'), self)
        self.exit.setStatusTip(self.tr('Quitter l\'application'))
        self.connect(self.exit, QtCore.SIGNAL('triggered()'),
                     QtCore.SIGNAL('exit()'))

        self.check_a = QtGui.QAction(QtGui.QIcon(Config.ICONS_DIR+'/check.png'),
                                   self.tr('Rechercher les mises à jour'), self)
        self.check_a.setStatusTip(self.tr('Rechercher les mises à jour'))
        self.connect(self.check_a, QtCore.SIGNAL('triggered()'),
                     QtCore.SIGNAL('check()'))

        self.prefs = QtGui.QAction(QtGui.QIcon(Config.ICONS_DIR+'/prefs.png'),
                                   self.tr('Préférences'), self)
        self.prefs.setStatusTip(self.tr('Préférences'))
        self.connect(self.prefs, QtCore.SIGNAL('triggered()'),
                     self.prefsDial)

        self.about = QtGui.QAction(QtGui.QIcon(Config.ICONS_DIR+'/about.png'),
                                   self.tr('À propos'), self)
        self.about.setStatusTip(self.tr('À propos'))
        self.connect(self.about, QtCore.SIGNAL('triggered()'), Dials.aboutDial)

        self.connect(self.tray, QtCore.SIGNAL('activated(QSystemTrayIcon::ActivationReason)'),
                     self.onClick)

    def onClick(self, reason):
        if reason == QtGui.QSystemTrayIcon.Trigger:
            self.emit(QtCore.SIGNAL('check()'))

    def createTrayIcon(self):
        self.tray = QtGui.QSystemTrayIcon(QtGui.QIcon(Config.ICONS_DIR+'/pacman.png'))
        self.tray.setToolTip('Update Checker')

    def setIcon(self, file):
        self.tray.setIcon(QtGui.QIcon(file))

    def createContextualMenu(self):
        menu = QtGui.QMenu(self)

        menu.addAction(self.check_a)
        menu.addAction(self.prefs)
        menu.addAction(self.about)
        menu.addSeparator()
        menu.addAction(self.exit)

        self.tray.setContextMenu(menu)

    def show(self):
        self.tray.show()

    def prefsDial(self):
        dial = Dials.PrefsDial()
        self.connect(dial, QtCore.SIGNAL('accepted()'),
                     QtCore.SIGNAL('prefsModified()'))
        dial.run()
